<template>
  <div class="container">
    <el-input
        type="textarea"
        class="textarea left"
        :row="row"
        v-model="leftText"
    />
    <el-input
        type="textarea"
        class="textarea right"
        :row="row"
        v-model="rightText"
    />
  </div>
  <el-footer>
    <el-row :gutter="5">
      <el-col :span="4">
        <el-select v-model="dbType" placeholder="数据库" filterable>
          <el-option v-for="item in dbOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="fileType" placeholder="生成类型" filterable>
          <el-option v-for="item in generatorOptions" :key="item.value" :label="item.label" :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button @click="copy" type="primary">
          复制
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="generator" type="primary">
          生成
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="download" type="primary">
          下载
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="configVisible = true" type="primary">
          配置
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="templateVisible = true" type="primary">
          模板
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="typeMappingVisible = true" type="primary">
          类型映射
        </el-button>
      </el-col>
    </el-row>
  </el-footer>
  <config-dialog
      :config-visible="configVisible"
      @handle-dialog-close="handleDialogClose"
      @save-config="saveConfig"
      :config="config"
  />
  <ejs-template
      :template-visible="templateVisible"
      @handle-dialog-close="handleDialogClose"
      :generator-options="generatorOptions"
  />
  <type-mapping
      :type-mapping-visible="typeMappingVisible"
      :db-options="dbOptions"
      @handle-dialog-close="handleDialogClose"
  />
</template>
<script>
import {convertTableInfo, copy2Clipboard, getEntityImportList, getXmlData, strIsBlank} from "../../utils/util.js";
import {parseMysql} from "../../utils/sqlParser.js";
import ConfigDialog from "../dialog/ConfigDialog.vue";
import EjsTemplate from "../dialog/EjsTemplate.vue";
import TypeMapping from "../dialog/TypeMapping.vue";
import ejs from "ejs";
import {formatJavaCode, formatXmlCode} from "../../api/formatter.js";
import {getTemplateByType} from "../../api/ejsGenerator.js";

export default {
  name: 'SqlGenerator',
  components: {TypeMapping, EjsTemplate, ConfigDialog},
  props: {
    dbOptions: Array,
    generatorOptions: Array,
    prefix: String,
    suffix: String
  },
  data() {
    return {
      row: 10,
      leftText: '',
      rightText: '',
      dbType: 'mysql',
      fileType: 'Entity',
      tableInfo: {},
      configVisible: false,
      templateVisible: false,
      typeMappingVisible: false,
      config: {},
      afterConvert: {}
    }
  },
  created() {
    this.config = JSON.parse(localStorage.getItem('config'))
  },
  methods: {
    copy() {
      copy2Clipboard(this)
      this.parseTableInfo()
    },
    parseTableInfo() {
      switch (this.dbType) {
        case 'mysql':
          this.parseMysql()
          break;
        case 'oracle':
          this.parseOracle()
          break;
        case 'postgresql':
          this.parsePostgresql()
          break;
        default:
          this.$message.error('当前类型未实现')
      }
    },
    parseMysql() {
      this.leftText = this.leftText.replaceAll('`', '')
      this.tableInfo = parseMysql(this.leftText);

    },
    parseOracle() {
      this.$message.error('当前类型不支持')
    },
    parsePostgresql() {
      this.$message.error('当前类型不支持')
    },
    handleDialogClose(type) {
      if (type === 'config') {
        this.configVisible = false
      }
      if (type === 'template') {
        this.templateVisible = false
      }
      if (type === 'typeMapping') {
        this.typeMappingVisible = false
      }
    },
    saveConfig(config) {
      this.config = config
      localStorage.removeItem('config')
      localStorage.setItem('config', JSON.stringify(this.config))
      this.configVisible = false
    },
    generator() {
      if (strIsBlank(this.config.basePackage) || strIsBlank(this.config.author)) {
        this.$message.error('请先填写配置信息')
        this.configVisible = true
      } else {
        if (this.fileType === 'Result') {
          this.generateResult()
        } else if(this.fileType === 'SearchButtons'){
          this.generateSearchButtons()
        }else {
          this.parseTableInfo()
          this.afterConvert = convertTableInfo(this.tableInfo, this.config, this.dbType)
          if (this.fileType !== '') {
            switch (this.fileType) {
              case 'Entity':
                this.generateEntity();
                break;
              case 'Mapper':
                this.generateMapper();
                break;
              case 'Controller':
                this.generateController();
                break;
              case 'Service':
                this.generateService();
                break;
              case 'Impl':
                this.generateImpl();
                break;
              case 'Xml':
                this.generateXml();
                break;
              case 'MockJs':
                this.generateMockJs();
                break;
              case 'ApiJs':
                this.generateApiJs();
                break;
              case 'IndexVue':
                this.generateIndexVue();
                break;
              case 'FormVue':
                this.generateFormVue();
                break;
              case 'Result':
                this.generateResult()
                break;
              case "SearchButtons":
                this.generateSearchButtons()
                break;
              default:
                this.rightText = '类型错误'
            }
          } else {
            this.$message.error('请选择生成文件类型')
          }
        }
      }
    },
    generateEntity() {
      const template = getTemplateByType(this.fileType);
      const data = {
        ...this.afterConvert,
        entityImportList: getEntityImportList(this.config),
        pattern: this.config.dateType === 'LocalDate' ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss'
      }
      this.rightText = formatJavaCode(ejs.render(template, data))
    },
    generateMapper() {
      const template = getTemplateByType(this.fileType);
      this.rightText = formatJavaCode(ejs.render(template, this.afterConvert))
    },
    generateController() {
      const template = getTemplateByType(this.fileType)
      this.rightText = formatJavaCode(ejs.render(template, this.afterConvert))
    },
    generateService() {
      const template = getTemplateByType(this.fileType)
      this.rightText = formatJavaCode(ejs.render(template, this.afterConvert))
    },
    generateImpl() {
      const template = getTemplateByType(this.fileType)
      this.rightText = formatJavaCode(ejs.render(template, this.afterConvert))
    },
    generateXml() {
      this.rightText = formatXmlCode(ejs.render(getTemplateByType(this.fileType), {
        ...this.afterConvert,
        xml: getXmlData(this.afterConvert, this.dbType, this.prefix, this.suffix)
      }))
    },
    generateMockJs() {
      const template = getTemplateByType(this.fileType)
      this.rightText = ejs.render(template, this.afterConvert)
    },
    generateApiJs() {
      const template = getTemplateByType(this.fileType)
      this.rightText = ejs.render(template, this.afterConvert)
    },
    generateIndexVue() {
      const template = getTemplateByType(this.fileType)
      this.rightText = formatXmlCode(ejs.render(template, this.afterConvert))
    },
    generateFormVue() {
      const template = getTemplateByType(this.fileType)
      this.rightText = formatXmlCode(ejs.render(template, this.afterConvert))
    },
    generateResult() {
      debugger
      const result = ejs.render(getTemplateByType(this.fileType), {
        config: this.config
      })
      this.rightText = formatJavaCode(result)
    },
    download() {
      this.$message.error('暂时没实现使用fs模块下载的功能')
    },
    template() {

    },
    generateSearchButtons() {
      const template = getTemplateByType(this.fileType)
      this.rightText = ejs.render(template, {})
    }
  }
}
</script>
<style></style>
